<?php
/* ***************************************
 * Copyright Reserved (C) Compass Team   **
 * @Procedure: class of Form Operation **
 * @Designer: Cavin  Zhang            **
 * @Date: 22 Dec, 2011           **
 * *****************************/
class overseaFormConfigured {
	//TODO - Insert your code here
	private $tdColumnArray = array('showName'=>'','inputType'=>'','inputName'=>'','defaultVal'=>'',
									'isMustInput'=>'','styleIdName'=>'');
	public $eachTdWidth = array();
	public $eachTdAlign = array();
	private $columNumber;
	public $tableHeader;
	public $showName;
	public $inputType;
	public $inputName;
	public $defaultVal;
	public $isMustInput;
	public $styleIdName;
	public $extraUsage;
	
	function __construct($tableHeader) {
		//$this -> columNumber = count($columnWidth);
		$this -> tableHeader = $tableHeader;
			//$eachTdAlign[] = 'left';

	}
	
	
	/*
	 * Function: display the input area of the form by using table
	 * Header Format: show name#@:form type#@:form name#@:default value#@:Y or N(append red star):css id name
	 * 				the same as the tdColumnArray
	 */
	public function executeTableLayout()
	{
		$rowsArr = explode('|-|', $this->tableHeader);
		$rowsCount = count($rowsArr);
		$tBody = '';
		for ($i = 0; $i < $rowsCount; $i++)
		{
			$j = 0;
			$tempArr = explode('#@:',$rowsArr[$i]);
			list($this -> showName,$this ->inputType,$this ->inputName,$this ->defaultVal,$this ->isMustInput) = $tempArr;
			$tBody .= "<tr><td align='".$this->eachTdAlign[0]."' width='".$this->eachTdWidth[0]."' valign='top'>".$this->appendRedStar($this ->isMustInput).$this->showName."</td>" ; 
			$tBody .= "<td align='".$this->eachTdAlign[1]."' width='".$this->eachTdWidth[1]."' valign='top'>".$this->getFormType($this->inputType).$this->appendWarningPic($this->isMustInput,$this->inputName)."</td></tr>";
		}
		return $tBody;
	}
	
	protected function getFormType($type)
	{
		
		switch ($type)
		{
			case 'select':
				return $this->appendSelection($this ->extraUsage,$this->inputName,$this->defaultVal);
				break;
			case 'textarea':
				break;
			case 'radio':
				return $this->appendRadioBox($this ->extraUsage,$this->inputName,$this->defaultVal);
				break;
			case 'custom':
				return $this->defaultVal;
				break;
			default:
				return "<input type='text' name='".$this->inputName."' value='".$this->defaultVal."' id='".$this->inputName."'></input>";
				break;			
		}	
	}
	
	public function appendSelection($valArr,$name,$defaultVal='')
	{
		$selection = "<select name='".$name."' id='".$name."'>";
		if(is_array($valArr))
		{
			foreach ($valArr as $key => $val)
			{
				$selected = ($val == $defaultVal)? 'selected':'';
				$selection .= "<option value='".$key."' $selected>".$val."</option>";
			}
			$selection .= "</select>";
			return $selection;
		}
	}
	
	public function appendRadioBox($valArr,$name,$defaultVal='')
	{
		if(is_array($valArr))
		{
			$radio = "";
			foreach ($valArr as $key => $val)
			{
				if($defaultVal == $val)
					$radio .= "<input type='radio' value='$key' name='".$name."'  checked>".$val."</input>";
				else
					$radio .= "<input type='radio' value='$key' name='".$name."' >".$val."</input>";
			}
			return $radio;
		}
		
	}
	
	public function appendRedStar($YorN)
	{
		if($YorN == 'Y')
			return "<span class='redstar'>*</span> ";
	}
	
	public function appendWarningPic($YorN,$id)
	{
		if($YorN == 'Y')
			return "<span class='warnPic-".$id."'><img src='{domainPath}/img/wrg.gif'></span>";
	}
}

?>